//
//  MyBalanceController.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/4.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "MyBalanceController.h"
#import "MyBalanceListCell.h"
#import "MyBalanceGetMoneyCell.h"
#import "MyBalanceTopCell.h"
#import "MyBalanceModel.h"

#import "MyBalanceNoCell.h"
#import "GoToCongMoneyVC.h"
#import "GetMoneyController.h"
#import "BaseNavigationController.h"
@interface MyBalanceController ()<MyBalanceGetMoneyCellDelegate>

@property (nonatomic, strong) NSMutableArray<MyBalanceModel *> *dataModels;

@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, strong) NSString *pageCount;

@property (nonatomic, assign) NSString *totalCount;

@property (nonatomic, strong) NSString *sum;

@end

@implementation MyBalanceController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的余额";
    
    self.plainTableView.backgroundColor = viewBgGrey;
  
//    self.plainTableView.rowHeight = UITableViewAutomaticDimension;

//    self.plainTableView.estimatedRowHeight = 100;
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyBalanceListCell class]) bundle:nil] forCellReuseIdentifier:[MyBalanceListCell identifierStr]];

    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyBalanceGetMoneyCell class]) bundle:nil] forCellReuseIdentifier:[MyBalanceGetMoneyCell identifierStr]];
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyBalanceTopCell class]) bundle:nil] forCellReuseIdentifier:[MyBalanceTopCell identifierStr]];

    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyBalanceNoCell class]) bundle:nil] forCellReuseIdentifier:[MyBalanceNoCell identifierStr]];
    
    
    MJWeakSelf
    self.plainTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.plainTableView.mj_header beginRefreshing];
    
    

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(WXApiDidSuccessNotification) name:WXApiDidSuccessNotification object:nil];
    

    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(AlipayDidSuccessNotification) name:AlipayDidSuccessNotification object:nil];
    

}




- (void)WXApiDidSuccessNotification{
    
    [self loadData];
    
}



- (void)AlipayDidSuccessNotification{
    
    
   [self loadData];
    
}




- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}




//合租 . 东津世纪城 三室一厅

- (void)loadData{
    
    self.curpage = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"page"] = @"1";
    parameters[@"pagesize"] = @"20";
    parameters[@"uid"] = myViewModel.user.uid;
    
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Member/balanceList" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
//                        MYJSONSTR
            //            _model.lastString
            
//            [[responseObject[@"data"][@"data"] firstObject]  propertyCode];
            
            weakSelf.pageCount = responseObject[@"data"][@"page_count"];
            
            weakSelf.totalCount = responseObject[@"data"][@"total_count"];
            
            weakSelf.sum = responseObject[@"data"][@"sum"];
            
            weakSelf.dataModels = [MyBalanceModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
            
            for (MyBalanceModel *model in weakSelf.dataModels ) {
                
                model.sum = weakSelf.sum;
            }
  
//[weakSelf.plainTableView.mj_footer resetNoMoreData];
            
            if ([weakSelf.totalCount isEqualToString:@"0"]) {
                
                
                
            } else {
                
                weakSelf.plainTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
                    
                    [weakSelf loadMoreData];
                    
                }];
                
                if ([weakSelf.pageCount isEqualToString:@"1"]) {
                    
                    [weakSelf.plainTableView.mj_footer endRefreshingWithNoMoreData];
                    
                }
            }
            
            [weakSelf.plainTableView.mj_header endRefreshing];
            
            
            
            [weakSelf.plainTableView reloadData];
            
            
        } else {
            
            [weakSelf.plainTableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
    }];
    
    
    
    
    
}




- (void)loadMoreData{
    
    self.curpage ++;
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pagesize"] = @"20";
    parameters[@"uid"] = myViewModel.user.uid;
    parameters[@"page"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"Member/balanceList" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            NSMutableArray<MyBalanceModel *> *temp = [MyBalanceModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
            
            for (MyBalanceModel *model in temp) {
                
                model.sum = weakSelf.sum;
                
            }
            
            
            [weakSelf.dataModels addObjectsFromArray:temp];
            
            
            if (weakSelf.pageCount.integerValue == self.curpage) {
                
                
                [weakSelf.plainTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [weakSelf.plainTableView.mj_footer endRefreshing];
                
                
            }
            
            [weakSelf.plainTableView reloadData];
            
        }else{
            
            [weakSelf.plainTableView.mj_footer endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
    }];
    
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataModels == nil) {
        
        return 0;
        
    } else {
        
        return 2;
        
    }
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (self.dataModels == nil) {
        
        return 0;
        
    } else {
        
        if (section == 0) {
            return 2;
        } else {
            
            if (self.dataModels.count == 0) {
                
                return 1;
                
            } else {
                
                return self.dataModels.count;
            }
            
        }
        
        
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            MyBalanceTopCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyBalanceTopCell identifierStr]];
            
            tableCell.sum = self.sum;
            
            return tableCell;
            
        } else {
            
            MyBalanceGetMoneyCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyBalanceGetMoneyCell identifierStr]];
            
            tableCell.delegate = self;
            
            return tableCell;
        }
        
    } else {
        
        if (self.dataModels.count == 0) {
            
            MyBalanceNoCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyBalanceNoCell identifierStr]];
            
            
            return tableCell;
            
        } else {
            
            MyBalanceListCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyBalanceListCell identifierStr]];
            
            tableCell.model = self.dataModels[indexPath.row];
            
            
            return tableCell;
            
            
        }
        
       
    }
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 72;
        } else {
            
            return 90;
        }
    } else {
        
        if (self.dataModels.count == 0) {
            CGFloat tempH = IPhoneX ? KScreenH - 286 : KScreenH - 262;
            return tempH;
        } else {
            return 72;
        }
        
        
    }
    
    
}





- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    if (section == 1) {
        
        return 36;

    } else {
        
        return 0.00001;

    }
    
    
    
    
}



- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    UIView *temp = [[UIView alloc]init];
    temp.backgroundColor = [UIColor whiteColor];
    return temp;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 1) {
        
        UIView *temp = [[UIView alloc]init];
        temp.backgroundColor = viewBgGrey;
        UILabel *label = [[UILabel alloc]init];
        label.text = @"明细";
        label.font = MYFONT(16);
        label.frame = CGRectMake(20, 0, 100, 36);
        [temp addSubview:label];
        return temp;
        
       
        
    } else {
        
        UIView *temp = [[UIView alloc]init];
        temp.backgroundColor = [UIColor whiteColor];
        return temp;
        
    }
    
  
    
    
    
}




- (void)getMoneyBtnDidClick{
    
    
    GetMoneyController *temp = [[GetMoneyController alloc]init];
    
//    BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:temp];
    
//    nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    
//    [self presentViewController:nav animated:YES completion:nil];
    
    [self.navigationController pushViewController:temp animated:YES];
    
    
}




- (void)getToCongMoneyBtnDidClick{
    
    
    GoToCongMoneyVC *temp = [[GoToCongMoneyVC alloc]init];
    
    //    BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:temp];
    
    //    nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    
    //    [self presentViewController:nav animated:YES completion:nil];
    
    [self.navigationController pushViewController:temp animated:YES];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
